package com.zd.application.service;

import java.util.Hashtable;
import java.util.Observable;
import java.util.Observer;

import org.apache.commons.beanutils.BeanUtils;

import com.zd.application.entity.User;

public class UserHandler extends Observable implements Observer {
	
	/**
	 * 自己
	 */
	private User myself = new User();

	/**
	 * 改变状态的所有用户
	 */
	private Hashtable<String, User> changeUsers = new Hashtable<String, User>();  
	
	/**
	 * 改变了自身的状态
	 * @param user
	 */
	public void change() {
		User clone = new User();
		try {
			BeanUtils.copyProperties(clone, myself);
			setChanged();		
			notifyObservers(clone);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 改变状态的用户
	 */
	@Override
	public void update(Observable o, Object object) {
		User user = (User)object;
		synchronized (changeUsers) {
			//changeUsers.put(user.getUid(), user);		
		}
	}

	/**
	 * 获取所有改变状态的用户
	 * @return
	 */
	public User[] getChanges() {
		User[] users = null;
		synchronized (changeUsers) {
			users = changeUsers.values().toArray(new User[0]);
			changeUsers.clear();
		}
		return users;
	}
	
	/**
	 * 获取自己的信息
	 * @return
	 */
	public User getMyself() {
		return myself;
	}
}
